package com.xiaohua.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.xiaohua.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.xiaohua.system.domain.XhFiles;
import com.xiaohua.system.mapper.XhOperationsMapper;
import com.xiaohua.system.domain.XhOperations;
import com.xiaohua.system.service.IXhOperationsService;

/**
 * 操作记录Service业务层处理
 * 
 * @author 小花
 * @date 2025-03-22
 */
@Service
public class XhOperationsServiceImpl implements IXhOperationsService 
{
    @Autowired
    private XhOperationsMapper xhOperationsMapper;

    /**
     * 查询操作记录
     * 
     * @param logId 操作记录主键
     * @return 操作记录
     */
    @Override
    public XhOperations selectXhOperationsByLogId(Long logId)
    {
        return xhOperationsMapper.selectXhOperationsByLogId(logId);
    }

    /**
     * 查询操作记录列表
     * 
     * @param xhOperations 操作记录
     * @return 操作记录
     */
    @Override
    public List<XhOperations> selectXhOperationsList(XhOperations xhOperations)
    {
        return xhOperationsMapper.selectXhOperationsList(xhOperations);
    }

    /**
     * 新增操作记录
     * 
     * @param xhOperations 操作记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertXhOperations(XhOperations xhOperations)
    {
        int rows = xhOperationsMapper.insertXhOperations(xhOperations);
        insertXhFiles(xhOperations);
        return rows;
    }

    /**
     * 修改操作记录
     * 
     * @param xhOperations 操作记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateXhOperations(XhOperations xhOperations)
    {
        xhOperationsMapper.deleteXhFilesByFileId(xhOperations.getLogId());
        insertXhFiles(xhOperations);
        return xhOperationsMapper.updateXhOperations(xhOperations);
    }

    /**
     * 批量删除操作记录
     * 
     * @param logIds 需要删除的操作记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteXhOperationsByLogIds(Long[] logIds)
    {
        xhOperationsMapper.deleteXhFilesByFileIds(logIds);
        return xhOperationsMapper.deleteXhOperationsByLogIds(logIds);
    }

    /**
     * 删除操作记录信息
     * 
     * @param logId 操作记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteXhOperationsByLogId(Long logId)
    {
        xhOperationsMapper.deleteXhFilesByFileId(logId);
        return xhOperationsMapper.deleteXhOperationsByLogId(logId);
    }

    /**
     * 新增文件管理信息
     * 
     * @param xhOperations 操作记录对象
     */
    public void insertXhFiles(XhOperations xhOperations)
    {
        List<XhFiles> xhFilesList = xhOperations.getXhFilesList();
        Long logId = xhOperations.getLogId();
        if (StringUtils.isNotNull(xhFilesList))
        {
            List<XhFiles> list = new ArrayList<XhFiles>();
            for (XhFiles xhFiles : xhFilesList)
            {
                xhFiles.setFileId(logId);
                list.add(xhFiles);
            }
            if (list.size() > 0)
            {
                xhOperationsMapper.batchXhFiles(list);
            }
        }
    }
}
